<?php

namespace app\index\controller;

class Article extends Base
{
//前置方法
    protected $beforeActionList = [
        // 'right_hot',
        'right' => ['only' => 'index,detailed'],
        // 'info_hot' => ['only' => 'detailed'],
        // 'articlemass' => ['only' => 'detailed'],
    ];

    //相关
    public function info_hot()
    {
        $data = $this->request->param();
        if (!empty($data['navid'])) {
            $where['cid'] = $data['navid'];
        } else {
            $where['cid'] = ['neq', '0'];
        }

        // 最新帖子
        $list = model('Article')
            ->where($this->datas)
            ->where($where)
            ->order('title deSC')
            ->cache(true)
            ->limit(5)
            ->select();
        $this->assign([
            'listinfo' => $list,
        ]);
    }


    //右边最热，最新
    public function right_hot()
    {
        $data = $this->request->param();
        if (!empty($data['id'])) {
            model('Article')->where('id', $data['id'])->setInc('scon', 1);
            $wechatkey = model('Article')->where('id', $data['id'])->value('wechatkey');
            $wechatkey = json_decode($wechatkey);
            $this->assign(['wechatkey'=>$wechatkey]);
        }

        //最新帖子
        $list = db('Article')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,a.title,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.id DESC,a.update_time DESC')
            ->limit(10)
            ->select();
        //最新回复帖子
        $hot = db('Article')
            ->alias('a')
            ->join('__NAV__ c', 'c.id= a.cid AND c.lang= a.lang AND c.status = a.status')
            ->field('a.id,a.cid,a.title,c.model,a.create_time')
            ->where('a.status', '1')
            ->order('a.update_time DESC,a.id DESC')
            ->limit(10)
            ->select();
        $this->assign([
            'hotten' => $hot,
            'listten' => $list,
        ]);
    }

    public function articlemass()
    {
        $list = db('articlemass')->where($this->datas)
            ->where('aid', $this->request->param('id'))
            ->order('id desc')
            ->limit(5)
            ->select();
        foreach ($list as $key => $v) {
            if (empty($v['uid'])) continue;
            $list[$key]['image'] = db('user')->where('id', $v['uid'])->value('image');
        }
        $this->assign([
            'list' => $list,
        ]);
    }


    public function aread($id)
    {
        $data = model('article')->where($this->datas)->find($id);
        $list = db('articlemass')->where($this->datas)->where('aid', $id)->paginate()->each(function ($item, $key) {
            $item['image'] = db('user')->where('id', $item['uid'])->value('image');
            return $item;
        });
        $this->assign([
            'data' => $data,
            'list' => $list,
        ]);
        return $this->fetch($this->template . $this->request->action() . '.html');
    }

    public function savemass()
    {
        $aa = session('User', '', 'User');

        if (empty($aa->id)) {
//            return ajax_info('请登录再留言！');
            $data['username'] = '匿名';
        } else {
            $data['username'] = $aa->name;
            $data['uid'] = $aa->id;
        }
        if ($this->request->isPost()) {
            $post = $this->request->param();
            $data['aid'] = $post['id'];
            $data['status'] = 0;
            $data['create_time'] = time();

            $data['center'] = $post['center'];
            $list = db('articlemass')->insert($data);
            if ($list) {
                model('article')->save(['update_time' => time()], ['id' => $post['id']]);
                return ajax_info('留言成功', 'u');
            } else {
                return ajax_info('重复留言');

            }
        }

    }
}
